package kr.co.casually.xml.parser;

import java.util.ArrayList;

import kr.co.casually.types.Item;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class NaverSearchListXMLHandler extends DefaultHandler {
	Item item = null;
	public ArrayList<Item> items = null;

	private StringBuilder currentValue;

	public ArrayList<Item> getList() {
		return items;
	}

	public void setList(ArrayList<Item> list) {
		items = list;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentValue = new StringBuilder();

		if (qName.equalsIgnoreCase("rss")) {
			if (items != null) {
				items.clear();
			} else {
				items = new ArrayList<Item>();
			}
			item = new Item();

		} else if (qName.equalsIgnoreCase("item")) {
			item = new Item();

		}
	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		/** set value */
		if (qName.equalsIgnoreCase("title")) {
			item.setTitle(currentValue.toString());

		} else if (qName.equalsIgnoreCase("link")) {
			item.setLink(currentValue.toString());

		} else if (qName.equalsIgnoreCase("description")) {
			item.setDescription(currentValue.toString());

		} else if (qName.equalsIgnoreCase("telephone")) {
			item.setTelephone(currentValue.toString());

		} else if (qName.equalsIgnoreCase("address")) {
			item.setAddress(currentValue.toString());

		} else if (qName.equalsIgnoreCase("mapx")) {
			item.setMapx(currentValue.toString());

		} else if (qName.equalsIgnoreCase("mapy")) {
			item.setMapy(currentValue.toString());

		} else if (qName.equalsIgnoreCase("item")) {
			items.add(item);
		}
	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// currentValue = new String(ch, start, length);

		currentValue.append(ch, start, length);

	}

}
